package servlet;

import bean.Result;
import biz.BizException;
import biz.userBiz;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/UserLogon.s")

public class UserLogonServlet extends HttpServlet {

     private userBiz biz=new userBiz();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doPost(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Result result = null;
            try {
                final String username = req.getParameter("username");
                final String pass = req.getParameter("pass");
                final String tel = req.getParameter("tel");
                final String email = req.getParameter("email");
                final HttpSession session = req.getSession();
                if ((username.trim().isEmpty()&&pass.trim().isEmpty()&&tel.trim().isEmpty()&&email.trim().isEmpty())==false){
                    biz.insert(username,tel,pass,email);
                    result=new Result(1,"注册成功",null);
                    session.setAttribute("username",username);
                }else {
                    result=new Result(0,"请将注册信息填写完整",null);
                }
            }catch (Exception e){
                result = new Result(0, e.getMessage(), null);
            }

        final String json = new Gson().toJson(result);
        resp.getWriter().append(json);

    }
}
